package com.wf.ew.storage.biz.impl;

import com.wf.ew.common.BaseDao;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.system.model.User;
import com.wf.ew.common.constant.UserConstant;
import com.wf.ew.storage.biz.WarehouseBiz;
import com.wf.ew.storage.model.Warehouse;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2019-11-18 12:36:44
 * @version ：1.0
 */
@Repository
public class WarehouseBizImpl extends BaseDao implements WarehouseBiz {

	@Override
	public String list(PageResult<T> page, Warehouse warehouse, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Warehouse> rm = new BeanPropertyRowMapper<Warehouse>(Warehouse.class);

		StringBuffer sql = new StringBuffer("select w.id,w.name,w.companyId");
        sql.append(",w.code,w.orderNum,w.remark,w.createTime");
		sql.append(",w.createUser,w.updateTime,w.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_warehouse w ");
        sql.append(" inner join sys_user suser on suser.id=w.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=w.companyId ");
        sql.append(" where w.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and w.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
    	if(Utils.isNotEmpty(warehouse)){
			if(Utils.isNotEmpty(warehouse.getName())){
				sql.append(" and (instr(w.name,:name)>0 or instr(w.code,:name)>0) ");
				map.put("name", warehouse.getName());
			}
			if(Utils.isNotEmpty(warehouse.getSort())&&Utils.isNotEmpty(warehouse.getOrder())){
				sql.append(" order by "+warehouse.getSort()+" "+warehouse.getOrder());
			} else {
				sql.append(" order by orderNum asc ");
			}
		}
		return jsonPage(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<Warehouse> list(Warehouse warehouse, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Warehouse> rm = new BeanPropertyRowMapper<Warehouse>(Warehouse.class);

        StringBuffer sql = new StringBuffer("select w.id,w.name,w.companyId");
        sql.append(",w.code,w.orderNum,w.remark,w.createTime");
		sql.append(",w.createUser,w.updateTime,w.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_warehouse w ");
        sql.append(" inner join sys_user suser on suser.id=w.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=w.companyId ");
        sql.append(" where w.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and w.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
    	if(Utils.isNotEmpty(warehouse)){
//			if(Utils.isNotEmpty(warehouse.getName())){
//				sql.append(" and instr(w.name,:name)>0 ");
//				map.put("name", warehouse.getName());
//			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Warehouse getById(Warehouse warehouse) {
		RowMapper<Warehouse> rm = new BeanPropertyRowMapper<Warehouse>(Warehouse.class);
		Map<String, String> map  = new HashMap<String, String>();

		StringBuffer sql = new StringBuffer("select w.id,w.name,w.companyId");
        sql.append(",w.code,w.orderNum,w.remark,w.createTime");
		sql.append(",w.createUser,w.updateTime,w.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_warehouse w ");
        sql.append(" inner join sys_user suser on suser.id=w.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=w.companyId ");
        sql.append(" where w.del=0 and cmy.del=0 and w.id=:id ");
		map.put("id", warehouse.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean add(Warehouse warehouse, User loginUser) {
		int count = 1;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into we_warehouse (");
        sql.append("name,companyId,code,orderNum");
		sql.append(",remark,createUser) values ( ");
        sql.append(":name,:companyId,:code,:orderNum");
		sql.append(",:remark,:createUser)");
        map.put("name", warehouse.getName());
        map.put("companyId", loginUser.getCompanyId());
        map.put("code", warehouse.getCode());
        map.put("orderNum", warehouse.getOrderNum()==null?null:warehouse.getOrderNum()+"");
        map.put("remark", warehouse.getRemark());
		map.put("createUser", loginUser.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(Warehouse warehouse, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update we_warehouse set ");
        sql.append("name=:name,code=:code,orderNum=:orderNum ");
		sql.append(",remark=:remark,updateTime=now(),updateUser=:updateUser where id=:id ");
        map.put("name", warehouse.getName());
        map.put("code", warehouse.getCode());
        map.put("orderNum", warehouse.getOrderNum()==null?null:warehouse.getOrderNum()+"");
        map.put("remark", warehouse.getRemark());
		map.put("updateUser", loginUser.getId());
		map.put("id", warehouse.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean delete(Warehouse warehouse, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(warehouse.getIds().length()>0){
			for(String ID : warehouse.getIds().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update we_warehouse set del=1" +
						",updateUser=:updateUser, updateTime=now() " 
						+ " where id =:id ");
				map.put("updateUser", loginUser.getId());
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
